package com.gw.booking.controller.backend;


import com.gw.booking.common.MessageCode;
import com.gw.booking.common.ResultObject;
import com.gw.booking.common.Util;
import com.gw.booking.dto.PageDto;
import com.gw.booking.dto.backend.*;
import com.gw.booking.entity.User;
import com.gw.booking.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.lang.reflect.InvocationTargetException;


@Api(tags = {"后台-用户管理"})
@Controller
@RequestMapping("/backend/users")
public class BUserController {
    @Autowired
    UserService userService;

    @ApiOperation(value = "登录")
    @RequestMapping(value = "login.do", method = {RequestMethod.POST})
    @ResponseBody
    public ResultObject<LoginDtoOut> login(HttpServletRequest request, @Valid @RequestBody LoginDto loginDto
            , BindingResult errors) {
        if (errors.hasErrors()) {
            return new ResultObject<>(MessageCode.CODE_PARAMETER_ERROR, null, errors.getAllErrors());
        }

        LoginDtoOut data = userService.txLogin(User.Type.backend, loginDto, request.getRemoteAddr(), request);

        return new ResultObject<>(MessageCode.CODE_SUCCESS, data);
    }

    @ApiOperation(value = "修改密码")
    @RequestMapping(value = "/changePassword.do", method = {RequestMethod.PUT})
    @ResponseBody
    public ResultObject changePassword(HttpServletRequest request
            , @Valid @RequestBody UserChangePasswordDto userChangePasswordDto
            , BindingResult errors) throws IllegalAccessException, InvocationTargetException {
        if (errors.hasErrors()) {
            return new ResultObject(MessageCode.CODE_PARAMETER_ERROR, null, errors.getAllErrors());
        }
        userService.txChangePassword(Util.getLoginToken(request).getUserId(), request.getRemoteAddr(), userChangePasswordDto);
        return new ResultObject(MessageCode.CODE_SUCCESS, null);
    }

    @ApiOperation(value = "添加用户")
    @RequestMapping(value = "/add.do", method = {RequestMethod.POST})
    @ResponseBody
    public ResultObject add(HttpServletRequest request
            , @Valid @RequestBody UserAddDto userAddDto
            , BindingResult errors) throws IllegalAccessException, InvocationTargetException {
        if (errors.hasErrors()) {
            return new ResultObject(MessageCode.CODE_PARAMETER_ERROR, null, errors.getAllErrors());
        }
        userService.txAdd(Util.getLoginToken(request), userAddDto, null);
        return new ResultObject(MessageCode.CODE_SUCCESS, null);
    }


    @ApiOperation(value = "删除用户")
    @RequestMapping(value = "/{userId}/delete.do", method = {RequestMethod.DELETE})
    @ResponseBody
    public ResultObject delete(HttpServletRequest request
            ,@ApiParam(value ="商家id", required = true) @PathVariable Integer userId)
            throws IllegalAccessException, InvocationTargetException {
        userService.txDelete(Util.getLoginToken(request), userId);
        return new ResultObject(MessageCode.CODE_SUCCESS, null);
    }

    @ApiOperation(value = "编辑用户")
    @RequestMapping(value = "update.do", method = {RequestMethod.PUT})
    @ResponseBody
    public ResultObject update(HttpServletRequest request, @Valid @RequestBody UserUptDto userUptDto
            , BindingResult errors) throws IllegalAccessException, InvocationTargetException {
        if (errors.hasErrors()) {
            return new ResultObject(MessageCode.CODE_PARAMETER_ERROR, null, errors.getAllErrors());
        }
        userService.txUpdate(Util.getLoginToken(request), userUptDto);
        return new ResultObject(MessageCode.CODE_SUCCESS, null);
    }

    @ApiOperation(value = "获取用户列表")
    @RequestMapping(value = "list.do", method = {RequestMethod.GET})
    @ResponseBody
    public ResultObject<PageDto<UserListDtoOut>> list(
            HttpServletRequest request
            , @ApiParam(value = "页数", required = true) @RequestParam Integer page
            , @ApiParam(value = "数量", required = true) @RequestParam Integer size
    ) throws IllegalAccessException, InvocationTargetException {
        return new ResultObject(MessageCode.CODE_SUCCESS, userService.txGetUsers(Util.getLoginToken(request), page, size));
    }


//    @ApiOperation(value = "获取用户信息")
//    @RequestMapping(value = "getPrivileges.do", method = {RequestMethod.GET})
//    @ResponseBody
//    public ResultObject<GetPrivilegesDtoOut> getPrivileges(HttpServletRequest request) throws IllegalAccessException, InvocationTargetException {
//        GetPrivilegesDtoOut dto = userService.txGetPrivileges(Util.getLoginToken(request));
//        return new ResultObject(MessageCode.CODE_SUCCESS, dto);
//
//    }



//    @ApiOperation(value = "获取用户信息")
//    @RequestMapping(value = "/{id}/detail.do", method = {RequestMethod.GET})
//    @ResponseBody
//    public ResultObject<UserUptDto> detail(
//      HttpServletRequest request
//      , @ApiParam(value = "id") @PathVariable Integer id
//    ) throws IllegalAccessException, InvocationTargetException {
//      UserListDtoOut userUptDto = userService.txGetOneUser(id);
//      return new ResultObject(MessageCode.CODE_SUCCESS, userUptDto);
//    }



}
